package com.briup.smartlabs.web.controller;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.briup.smartlabs.bean.SmartDevice;

import com.briup.smartlabs.common.utils.Response;
import com.briup.smartlabs.mapper.SmartDeviceMapper;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;


@Api(tags = "设备管理服务")
@RestController
public class DeviceController{
	
	@Autowired
	private SmartDeviceMapper deviceMapper;
	private String a;
	private String b;
	private String c;
	
	@ApiOperation(value="分页显示所有设备信息")
	@GetMapping("/device/by/page")
	public Response<PageInfo<SmartDevice>> findByPage(
		@RequestParam(defaultValue="5")int pageSize,
		@RequestParam(defaultValue="1")int pageNum){
		PageHelper.startPage(pageNum, pageSize, true);
		List<SmartDevice> devices=deviceMapper.selectAll();
		PageInfo<SmartDevice>pages=new PageInfo<>(devices);
		return Response.ok(pages);
	
	}
	

	
	@ApiOperation(value="返回所有设备信息")
	@GetMapping("/device/all")
	public Response<List<SmartDevice>>findAll(){
		List<SmartDevice>devices=deviceMapper.selectAll();
		return Response.ok(devices);
	}


	
	@DeleteMapping("/device/by/id")
	public Response<String>deleteById(Long id){
		
		if(id==null) {
			return Response.error("请输入要删除的设备id");
			
		}
		deviceMapper.deleteByPrimaryKey(id);
		return Response.ok("删除成功");
	}
	
	@ApiOperation(value="保存或者更新设备信息",
			notes="1.如果用户不包含设备id,保存！<br/>"
			+"2.如果参数包含设备id,更新！")
	@PostMapping("/device")
	public Response<String> saveOrUpdate(SmartDevice device){
		if(device.getId()==null) {
			//默认，如果数据没有传递主键值，就代表该数据在数据库中不存在，执行保存操作。
			device.setDeviceName(a);
			device.setDeviceType(b);
			device.setDeviceAddr(c);
			deviceMapper.insert(device);
		}else {
			deviceMapper.updateByPrimaryKey(device);
		}
		return Response.ok("操作成功！");
	}
	
	
	
	@ApiOperation(value="按照名字分页显示设备信息")
	@ApiImplicitParams({
		@ApiImplicitParam(name="name",value="设备名"),
		@ApiImplicitParam(name="pageNum",value="当前页码"),
		@ApiImplicitParam(name="pageSize",value="每页显示的个数"),
	})
	
	
	@GetMapping("/device/by/name")
	public Response<PageInfo<SmartDevice>> findByName(
			String name,
			@RequestParam(defaultValue = "1")int pageNum,
			@RequestParam(defaultValue = "5")int pageSize){
		PageHelper.startPage(pageNum, pageSize,true);
		if(name!=null&&!"".equals(name)){
			name="%"+name+"%";
		}
		List<SmartDevice> devices=deviceMapper.selestNameLike(name);
		PageInfo<SmartDevice> pages=new PageInfo<>(devices);
		return Response.ok(pages);
	}
	
	
	
	
	
	@ApiOperation(value="按照设备id批量删除设备信息！")
	@DeleteMapping("device/by/batch")
	public Response<String> deleteByBath(Long[] ids){
		if(ids==null||ids.length<=0) {
			return Response.error("请至少选择一个要删除的设备！");
		}
		
		for(Long id:ids) {
			deviceMapper.deleteByPrimaryKey(id);
		}
		
		return Response.ok("删除成功！");
		

	}
}
